package org.xczy.payment.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.xczy.payment.service.PaymentService;
import org.xczy.service.model.common.Result;
import org.xczy.service.model.entitys.pojo.Payment;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/payment")
public class PaymentController {

  @Resource
  private PaymentService paymentService;

  @Value("${server.port}")
  private String serverPort;

  @PostMapping
  public Result addPayment(@RequestBody Payment payment){
    int i = paymentService.addPayment(payment);
    if(i > 0){
      return Result.ok("添加成功");
    }
    return Result.fail("添加失败");
  }

  @DeleteMapping("/{id}")
  public Result deletePayment(@PathVariable("id") Long id){
    int i = paymentService.deletePayment(id);
    if(i > 0){
      return Result.ok("删除成功");
    }
    return Result.fail("删除失败");
  }

  @PutMapping
  public Result editPayment(@RequestBody Payment payment){
    int i = paymentService.editPayment(payment);
    if(i > 0){
      return Result.ok("修改成功");
    }
    return Result.fail("修改失败");
  }

  @GetMapping("/{id}")
  public Result getPayment(@PathVariable("id") Long id){
    Map<String, Object> map = new HashMap<>();
    map.put("payment", paymentService.getPayment(id));
    map.put("port", serverPort);
    return Result.ok(map);
  }

  @GetMapping
  public Result getPaymentAll(){
    Map<String, Object> map = new HashMap<>();
    map.put("list", paymentService.getPayment());
    return Result.ok(map);
  }
}
